package com.lovo.stock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.stock.bean.GoodBean;
import com.lovo.stock.bean.PageInfo;
import com.lovo.stock.constant.MessageCode;
import com.lovo.stock.service.IGoodService;
import com.lovo.stock.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *库存商品类
 */
@RestController
@RequestMapping("/good")
public class GoodController {
    @Autowired
    IGoodService goodService;

    /**
     * 分页查询商品
     * @param pageInfo
     * @return
     */
    @RequestMapping("/queryGoodByPage")
    public ResponseResult<IPage<GoodBean>> queryGoodByPage(@RequestBody PageInfo<GoodBean> pageInfo){
        ResponseResult<IPage<GoodBean>> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(goodService.queryGoodByPage(pageInfo));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 根据商品id查询用户
     * @param id
     * @return
     */
    @RequestMapping("/queryGoodById")
    public ResponseResult<GoodBean> queryGoodById(int id){
        ResponseResult<GoodBean> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(goodService.queryGoodById(id));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 修改价格
     * @param goodBean
     */
    @RequestMapping("/updateGood")
    public ResponseResult updateGood(@RequestBody GoodBean goodBean){
        ResponseResult responseResult=new ResponseResult<>();
        try {
            goodService.updateGood(goodBean);
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }
}
